<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :bodyStyle="bodyStyle"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内部编码">
            <a-input disabled placeholder="请输入内部编码" v-decorator.trim="['contractCode', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="合同号">
            <a-input disabled placeholder="请输入合同号" v-decorator.trim="['contractNum', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="产品名称">
            <!-- <a-input  placeholder="请输入产品名称" v-decorator.trim="['productName']" /> -->
            <a-select
              v-decorator.trim="['productName', validatorRules.name]"
              allowClear
              placeholder="选择"
              showSearch
              optionFilterProp="children"
            >
              <a-select-option v-for="(item, index) in PraductNameList" :key="index" :value="item.name">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="钢印号">
            <a-input placeholder="请输入钢印号" v-decorator.trim="['steelSeal', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="安装机组">
            <a-input placeholder="请输入安装机组" v-decorator.trim="['installUnit']" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="安装时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              v-decorator.trim="['installDate', validatorRules.name]"
              format="YYYY-MM-DD"
              placeholder="请输入安装时间"
            />
            <!-- <a-month-picker placeholder="请输入安装时间" style="width: 100%" v-decorator.trim="['installDate']" /> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="使用时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入使用时间" v-decorator.trim="['startupDate']" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="质保期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入质保期" v-decorator.trim="['warrantyPeriod']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
            <a-textarea :rows="2" placeholder="请输入备注" v-decorator.trim="['remark']" />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { getAction } from '@/api/manage'
import { AddInstall, checkSupplier } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'AddLogistics',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      record: null,
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.6 + 'px',
        'overflow-y': 'auto'
      },
      PraductNameList: [],
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入!' }
            // { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
            // { validator: this.validateSupplierName }
          ]
        }
      },
      url: {
        lists: '/contractProduct/getList'
      }
    }
  },
  mounted() {},
  methods: {
    add(record) {
      this.record = record
      this.edit(record)
    },
    getPraductNameList() {
      let params = {
        contractCode: this.model.contractCode
      }
      console.log(this.model.contractCode, 'this.model.contractCode')
      getAction(this.url.lists, params).then(res => {
        if (res.code === 200 || res.code === 10000) {
          this.PraductNameList = res.data
        }
      })
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.getPraductNameList()
      this.visible = true
      this.$nextTick(() => {
        //编辑
        if (record.steelSeal) {
          var installDate = this.$moment(this.model.installDate)
          this.form.setFieldsValue({ installDate: installDate })
          this.form.setFieldsValue({ steelSeal: this.model.steelSeal })
          this.form.setFieldsValue({ installUnit: this.model.installUnit })
          this.form.setFieldsValue({ remark: this.model.remark })
        }
        this.form.setFieldsValue(
          pick(
            this.model,
            'startupDate',
            'contractCode',
            'contractNum',
            'customerName',
            'productName',
            'warrantyPeriod'
          )
        )
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          console.log(this.model)

          //编辑
          if (this.model.steelSeal) {
            formData.id = this.model.id
          } else {
            formData.contractId = this.model.id
          }
          formData.contractCode = this.model.contractCode
          formData.contractNum = this.model.contractNum
          formData.customerName = this.model.customerName
          formData.installDate = this.$moment(formData.installDate).format('YYYY-MM-DD')
          let obj
          if (!this.model.id) {
            obj = AddInstall(formData)
          } else {
            obj = AddInstall(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped></style>
